package com.company.project.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.company.project.service.impl.WorkOrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.company.project.common.utils.DataResult;

import com.company.project.entity.WorkOrderEntity;
import com.company.project.service.WorkOrderService;



/**
 * 车间工作单
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2024-07-12 22:03:39
 */
@Controller
@RequestMapping("/")
public class WorkOrderController {
    @Autowired
    private WorkOrderService workOrderService;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/workOrder")
    public String workOrder(Model model) {
        JSONArray machines = new JSONArray();
        for (Map.Entry<String, String[]> entry : WorkOrderServiceImpl.machineMap.entrySet()) {
            JSONObject machineInfo = new JSONObject();
            machineInfo.put("workshop", entry.getKey());
            machineInfo.put("machines", entry.getValue());
            machines.add(machineInfo);
        }
        model.addAttribute("machines", machines);

        return "workorder/list";
    }

    @GetMapping("/sb1")
    @SaIgnore
    public String sb1(Model model) {
        JSONArray productionPlan=workOrderService.getOrders("sb1");
        model.addAttribute("productionPlan",productionPlan);
        model.addAttribute("title","丝包一车间生产进度表");
        return "workorder/workShop";
    }
    @GetMapping("/sb2")
    @SaIgnore
    public String sb2(Model model) {
        JSONArray productionPlan=workOrderService.getOrders("sb2");
        model.addAttribute("productionPlan",productionPlan);
        model.addAttribute("title","丝包二车间生产进度表");
        return "workorder/workShop";
    }
    @GetMapping("/sj1")
    @SaIgnore
    public String sj1(Model model) {
        JSONArray productionPlan=workOrderService.getOrders("sj1");
        model.addAttribute("productionPlan",productionPlan);
        model.addAttribute("title","烧结一车间生产进度表");
        return "workorder/workShop";
    }
    @GetMapping("/sj2")
    @SaIgnore
    public String sj2(Model model) {
        JSONArray productionPlan=workOrderService.getOrders("sj2");
        model.addAttribute("productionPlan",productionPlan);
        model.addAttribute("title","烧结二车间生产进度表");
        return "workorder/workShop";
    }
    @GetMapping("/jy1")
    @SaIgnore
    public String jy1(Model model) {
        JSONArray productionPlan=workOrderService.getOrders("jy1");
        model.addAttribute("productionPlan",productionPlan);
        model.addAttribute("title","挤压一车间生产进度表");
        return "workorder/workShop";
    }


    @ApiOperation(value = "查询分页数据")
    @PostMapping("workOrder/listByPage")
    @SaCheckPermission("workOrder:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody WorkOrderEntity workOrder){
        LambdaQueryWrapper<WorkOrderEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(null!=workOrder.getWorkshop()&&!"0".equals(workOrder.getWorkshop()), WorkOrderEntity::getWorkshop, workOrder.getWorkshop());
        queryWrapper.eq(null!=workOrder.getMachineNumber()&&!"0".equals(workOrder.getMachineNumber()), WorkOrderEntity::getMachineNumber, workOrder.getMachineNumber());
        queryWrapper.orderByAsc(WorkOrderEntity::getOrders);
        IPage<WorkOrderEntity> iPage = workOrderService.page(workOrder.getQueryPage(), queryWrapper);
        return DataResult.success(iPage);
    }


    @ApiOperation(value = "新增")
    @PostMapping("workOrder/add")
    @SaCheckPermission("workOrder:add")
    @ResponseBody
    public DataResult add(@RequestBody WorkOrderEntity workOrder){
            workOrderService.save(workOrder);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("workOrder/delete")
    @SaCheckPermission("workOrder:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
            workOrderService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("workOrder/update")
    @SaCheckPermission("workOrder:update")
    @ResponseBody
    public DataResult update(@RequestBody WorkOrderEntity workOrder){
            workOrderService.updateById(workOrder);
        return DataResult.success();
    }



}
